package com.winexpress.account;

import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "GoogleSignIn", urlPatterns = {"/googleSignIn"})
public class GoogleSignIn extends HttpServlet {

    public static final HttpTransport TRANSPORT = new NetHttpTransport();
    public static final JacksonFactory JSON_FACTORY = new JacksonFactory();
    private static final Gson GSON = new Gson();
    private GoogleClientSecrets clientSecrets;

    public static String CLIENT_ID;
    public static String CLIENT_SECRET;
    public static final String APPLICATION_NAME = "wineXpress";

    private void readSecrets() {
        try {
            String path = getServletConfig().getServletContext().getRealPath("/resources") + "/client_secrets.json";
            Reader reader = new FileReader(path);
            clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, reader);
            CLIENT_ID = clientSecrets.getWeb().getClientId();
            CLIENT_SECRET = clientSecrets.getWeb().getClientSecret();
        } catch (IOException e) {
            throw new Error("No client_secrets.json found", e);
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        readSecrets();
        String url = "https://accounts.google.com/o/oauth2/auth?" + "scope=email%20profile&" + "redirect_uri=https://localhost:8181/WineXpressClient/googleAuth&" + "response_type=code&" + "client_id=" + CLIENT_ID;
        response.sendRedirect(url);
    }

    static void getContent(InputStream inputStream, ByteArrayOutputStream outputStream)
            throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        int readChar;
        while ((readChar = reader.read()) != -1) {
            outputStream.write(readChar);
        }
        reader.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
